/**
 * 
 */
package uk.ac.manchester.cs.graph;

import java.util.HashMap;
import java.util.Map;


/**
 * A class with various utility methods
 * 
 * @author Pavel Klinov pklinov@cs.man.ac.uk, pklinov@clarkparsia.com
 */
public class Utils {

	/*
	 * Returns a bipartite graph representing the hypergraph
	 */
	public static <V, E> Graph<String, String> hyperGraphToOrdinaryGraph(Hypergraph<V, E> hyperGraph) {

		Graph<String, String> graph = new GraphImpl<String, String>();
		Map<V, String> prevVertices = new HashMap<V, String>();

		for( Hyperedge<V, E> hEdge : hyperGraph.getHyperedges() ) {

			graph.addVertex( hEdge.toString() );

			for( V hVert : hEdge.getVertices() ) {

				String vVertex = prevVertices.get( hVert );
				
				if (null == vVertex) {
					
					graph.addVertex( hVert.toString() );
					prevVertices.put( hVert, vVertex );
				}
				
				graph.addEdge( "", vVertex, hEdge.toString() );
			}
		}
		// TODO Should also take care or dangling vertices in the hypergraph. Ignore them for now
		return graph;
	}
}